package com.hnchances.service.impl;

import com.hnchances.mapper.StudentMapper;
import com.hnchances.mapper.UserMapper;
import com.hnchances.pojo.dto.UserDTO;
import com.hnchances.pojo.entity.Student;
import com.hnchances.pojo.entity.User;
import com.hnchances.pojo.vo.UserVO;
import com.hnchances.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    private final StudentMapper studentMapper;

    private final RedisTemplate redisTemplate;

    private static final String SMS_KEY = "SMS_CAPTCHA";

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper, RedisTemplate redisTemplate, StudentMapper studentMapper) {
        this.userMapper = userMapper;
        this.redisTemplate = redisTemplate;
        this.studentMapper = studentMapper;
    }

    @Override
    public void register(UserDTO userDTO) {
        // 1.判断是老师还是学生
        if (userDTO.getIdentity() == 1){
            // 老师
            // 向数据库中查询姓名名，判断是否已存在
            User selectUser = userMapper.selectByTruename(userDTO.getTruename());
            if (selectUser != null){
                // 已存在
                throw new RuntimeException("用户已存在");
            }

            // 判断验证码是否正确
            // 从redis中获取验证码
            String code = (String) redisTemplate.opsForValue().get(SMS_KEY);
            System.out.println("验证码："+code);
            // 判断验证码是否正确
            if (code == null){
                // 验证码过期
                throw new RuntimeException("验证码过期");
            }
            if (!code.equals(userDTO.getCode())){
                // 验证码不正确
                throw new RuntimeException("验证码不正确");
            }

            // 获取密码
            String password = userDTO.getPassword();
            // 对密码进行MD5加密
            password = DigestUtils.md5DigestAsHex(password.getBytes());

            // 创建user对象 并插入数据库
            User user = new User();
            user.setUsername(userDTO.getUsername());
            user.setPassword(password);
            user.setPhone(userDTO.getPhone());
            user.setTruename(userDTO.getTruename());
            user.setIdentity(userDTO.getIdentity());
            user.setClassid(userDTO.getClassid());
            user.setCourseid(userDTO.getCourseid());
            user.setCreatdatetime(LocalDateTime.now());
            user.setStatus(0);
            user.setVerify(0);
            user.setLoginip("127.0.0.1");
            user.setUpdatebyid(1L);
            userMapper.insert(user);
        }else {
            // 学生
            // 判断在学生表中是否存在该学生，如果不存在则无法注册
            Student selectStudent = studentMapper.selectByStudentId(userDTO.getStudentid());
            if (selectStudent == null){
                // 学生不存在
                throw new RuntimeException("学生不存在");
            }

            // 获取验证码 判断验证码是否正确
            String code = (String) redisTemplate.opsForValue().get(SMS_KEY);
            System.out.println("验证码："+code);
            if (code == null){
                // 验证码过期
                throw new RuntimeException("验证码过期");
            }
            if (!code.equals(userDTO.getCode())){
                // 验证码不正确
                throw new RuntimeException("验证码不正确");
            }

            // 获取密码
            String password = userDTO.getPassword();
            // 对密码进行MD5加密
            password = DigestUtils.md5DigestAsHex(password.getBytes());

            // 创建user对象 并插入数据库
            User user = new User();
            user.setUsername(userDTO.getUsername());
            user.setPassword(password);
            user.setPhone(userDTO.getPhone());
            user.setTruename(userDTO.getTruename());
            user.setIdentity(userDTO.getIdentity());
            user.setClassid(userDTO.getClassid());
            user.setStudentid(userDTO.getStudentid());
            user.setCreatdatetime(LocalDateTime.now());
            user.setUpdatedatetime(LocalDateTime.now());
            user.setStatus(0);
            user.setVerify(1);
            user.setLoginip("127.0.0.1");
            user.setUpdatebyid(1L);
            userMapper.insert(user);
        }
    }

    @Override
    public void update(User user) {
        // 获取用户密码
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setUsername(user.getUsername());
        user.setPhone(user.getPhone());
        user.setTruename(user.getTruename());
        user.setIdentity(user.getIdentity());
        user.setClassid(user.getClassid());
        user.setCourseid(user.getCourseid());
        user.setUpdatedatetime(LocalDateTime.now());
        user.setUpdatebyid(1L);
        user.setStatus(0);
        user.setVerify(1);
        user.setPassword(password);
        userMapper.update(user);
    }

    @Override
    public void del(Long id) {
        // 获取用户
        User selectById = userMapper.selectById(id);
        // 如果是学生 则无法删除用户
        if (selectById.getIdentity() == 0){
            throw new RuntimeException("学生无法删除");
        }
        selectById.setStatus(1);
        userMapper.update(selectById);
    }

    @Override
    public User selectById(Long id) {
        return userMapper.selectById(id);
    }
}
